package com.hgy.crowd.service.impl;

import com.hgy.crowd.api.MySQLRemoteService;
import com.hgy.crowd.api.RedisRemoteService;
import com.hgy.crowd.config.MessageProperties;
import com.hgy.crowd.po.MemberPO;
import com.hgy.crowd.service.MemberService;
import com.hgy.crowd.util.CrowdUtil;
import com.hgy.crowd.util.ResultEntity;
import com.hgy.crowd.vo.MemberLoginVO;
import com.hgy.crowd.vo.MemberVO;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author He
 * @Date 2020/7/1 15:26
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MessageProperties messageProperties;
    @Resource
    private RedisRemoteService redisRemoteService;
    @Resource
    private MySQLRemoteService mySQLRemoteService;

    @Override
    public ResultEntity registryMember(MemberVO memberVO) {
        String phoneNum = memberVO.getPhoneNum();
        ResultEntity<String> code = redisRemoteService.getStringValueByKeyRemote(phoneNum);

        if (ResultEntity.SUCCESS.equals(code.getResult())){
            if (Objects.equals(code.getData(),memberVO.getCode())){
                BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
                String encode = encoder.encode(memberVO.getUserpswd());
                redisRemoteService.removeRedisKeyRemote(phoneNum);
                MemberPO memberPO=new MemberPO(null,
                        memberVO.getLoginacct(),
                        encode,
                        memberVO.getUsername(),
                        memberVO.getEmail(),
                        null,
                        null,
                        null,
                        null,
                        null);
                return mySQLRemoteService.saveMember(memberPO);
            }else {
                return ResultEntity.failed("验证码填写错误，请检查后重新注册");
            }
        }else {
            //如果Redis查询失败则返回失败信息
            code.setMessage("验证码失效或手机号错误");
            return code;
        }
    }

    @Override
    public ResultEntity sendMessageService(String phoneNum) {
        String code;
        ResultEntity resultEntity = CrowdUtil.sendMessage(messageProperties.getHost(),
                messageProperties.getPath(),
                messageProperties.getAppCode(),
                messageProperties.getSign(),
                messageProperties.getSkin(),
                phoneNum);
        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){
            code= (String) resultEntity.getData();
            redisRemoteService.setStringKeyAndValueWithTimeoutRemote(phoneNum,code,15L, TimeUnit.MINUTES);
        }
        return resultEntity;
    }

    @Override
    public ResultEntity login(String loginacct, String userpswd) {
        MemberLoginVO memberLoginVO;
        ResultEntity<MemberPO> member = mySQLRemoteService.getMemberByLoginAccountRemote(loginacct);
        if (ResultEntity.SUCCESS.equals(member.getResult())){
            MemberPO data = member.getData();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            if (bCryptPasswordEncoder.matches(userpswd, data.getUserpswd())){
                memberLoginVO=new MemberLoginVO(data.getUsername(),data.getEmail(),data.getId());
                return ResultEntity.successWithData(memberLoginVO);
            }else {
                return ResultEntity.failed("密码输入错误");
            }
        }
        return member;
    }
}
